﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Skyworth.CRM.Service.Entity;
using Skyworth.CRM.Service.IDAL;

namespace Skyworth.CRM.DAL
{
   public class BrandDAL:SuperDataBase,IBrandDAL
    {
       public void Add(Brand model)
       {
           StringBuilder strSql = new StringBuilder();
           strSql.Append(" insert into T_BasicData_Brand( ");
           strSql.Append(" BrandCode,BrandName,Description ) ");
           strSql.Append(" values ( ");
           strSql.Append(" @brandCode,@brandName,@description) ");
           //Database db = DatabaseFactory.CreateDatabase();               
           DbCommand cmd = DB.GetSqlStringCommand(strSql.ToString());
           DB.AddInParameter(cmd, "@brandCode", DbType.String, model.BrandCode);
           DB.AddInParameter(cmd, "@brandName", DbType.String, model.BrandName);
           DB.AddInParameter(cmd, "@description", DbType.String, model.Description);
           DB.ExecuteNonQuery(cmd);
       }

       public void update(Brand model)
       {
           StringBuilder strSql = new StringBuilder();
           strSql.Append(" update T_BasicData_Brand set ");
           strSql.Append(" BrandCode=@brandCode,");
           strSql.Append(" BrandName=@brandName,");
           strSql.Append(" Description=@description ");
           strSql.Append(" where BrandId=@brandId ");

           DbCommand cmd = DB.GetSqlStringCommand(strSql.ToString());
           DB.AddInParameter(cmd, "@brandId", DbType.Int32, model.BrandId);
           DB.AddInParameter(cmd, "@brandCode", DbType.String, model.BrandCode);
           DB.AddInParameter(cmd, "@brandName", DbType.String, model.BrandName);
           DB.AddInParameter(cmd, "@description", DbType.String, model.Description);
           DB.ExecuteNonQuery(cmd);
       }

       public void Delete(int brandId)
       {
           string sql = "delete [T_BasicData_Brand] where brandId=@brandId ";
           System.Data.Common.DbCommand sqlcommand = DB.GetSqlStringCommand(sql);
           DB.AddInParameter(sqlcommand, "@brandId", DbType.Int32,brandId);
           DB.ExecuteNonQuery(sqlcommand);
       }

       public Brand GetBrandById(int brandId)
       {
           Brand model = null;
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select  top 1 BrandId,BrandCode,BrandName,Description from T_BasicData_Brand ");
                strSql.Append(" where BrandId=@brandId");
                DbCommand cmd = DB.GetSqlStringCommand(strSql.ToString());
                DB.AddInParameter(cmd, "@brandId", DbType.Int32, brandId);
                using (IDataReader dr = DB.ExecuteReader(cmd))
                {
                    while (dr.Read())
                    {
                        model = new Brand();
                        if (dr["BrandId"].ToString() != "")
                        {
                            model.BrandId = int.Parse(dr["BrandId"].ToString());
                        }
                        model.BrandCode = dr["BrandCode"].ToString();
                        model.BrandName = dr["BrandName"].ToString();
                        model.Description = dr["Description"].ToString();
                    }
                }
                return model;
           
       }

       public DataTable GetBrand(string strWhere, Hashtable parameters)
       {
           string sql = "SELECT BrandId,BrandCode,BrandName,Description FROM [T_BasicData_Brand] where 1=1 ";
           sql += strWhere;
           System.Data.Common.DbCommand sqlcommand = DB.GetSqlStringCommand(sql);
           if (parameters != null && parameters.Count != 0)
           {
               foreach (DictionaryEntry de in parameters)
               {
                   DB.AddInParameter(sqlcommand, de.Key.ToString(), DbType.String);
                   DB.SetParameterValue(sqlcommand, de.Key.ToString(), de.Value.ToString());
               }
           }
           DataSet ds = DB.ExecuteDataSet(sqlcommand); 
           return ds.Tables[0];
       }
    }
}
